package com.bynk.bynk_demo.config.interceptor;

import com.bynk.bynk_demo.model.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        //每一个项目对于登陆的实现逻辑都有所区别，我这里使用最简单的Session提取User来验证登陆。
        HttpSession session = request.getSession();

        //这里的User是登陆时放入session的
        User user = (User) session.getAttribute("user");

        //如果session中没有user，表示没登陆
        if (null != user){
            System.out.println("LoginInterceptor");
            return true;
        }
        else {
            System.out.println("unpass");
            response.sendRedirect("/html/login.html");
            return false;
        }

    }
}
